<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form action="/login" id="form1">
        <!-- autocomplete="off" 阻止输入框自动填充 -->
        <input type="text" name="username" autocomplete="off">
        <input type="password" name="password">
        <button type="submit">提交</button>
    </form>

    <script>
        var form = document.querySelector("#form1");

        form.addEventListener("submit", function (e) {
            //阻止默认提交行为
            e.preventDefault();
            //创建FormData快速获取表单数据
            var fd = new FormData(form);
            //发送ajax
            var xhr = new XMLHttpRequest();
            xhr.open("POST", "http://www.liulongbin.top:3006/api/formdata");
            //发送表单数据
            xhr.send(fd);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    console.log(JSON.parse(xhr.responseText));
                }
            }

        })

    </script>

</body>

</html>